<template>
  <div class="add">
    <el-tabs v-model="activeName">
      <el-tab-pane name="first">
        <section class="append">
          <ul>
            <li>
              <span>题目类型:</span>
              <el-select v-model="optionValue" placeholder="请选择题型" class="w150">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </li>
            <li>
              <span>科目:</span>
              <el-input v-model="form.subject" class="w150" placeholder="请输入科目" />
            </li>
            <li>
              <span>分值:</span>
              <el-input-number v-model="form.score" :min="0" class="w150" />
            </li>
            <li>
              <span>备注:</span>
              <el-input v-model="form.remark" class="w150" placeholder="请输入备注" />
            </li>
          </ul>

          <!-- 选择题部分 -->
          <div class="change" v-if="optionValue === '选择题'">
            <div class="title">
              <el-tag>题目:</el-tag>
              <el-input
                type="textarea"
                rows="2"
                v-model="form.content"
                placeholder="请输入题目内容"
                resize="none"
                class="answer" />
            </div>
            <div class="options">
              <ul>
                <li v-for="(opt, idx) in choiceOptions" :key="idx">
                  <el-tag type="success">{{ letters[idx] }}</el-tag>
                  <el-input
                    placeholder="请输入选项内容"
                    v-model="choiceOptions[idx]"
                    clearable />
                </li>
              </ul>
            </div>
            <div class="correct">
              <span>正确答案:</span>
              <el-select v-model="form.answer" placeholder="请选择正确答案" class="w150">
                <el-option
                  v-for="(l) in letters"
                  :key="l"
                  :label="l"
                  :value="l" />
              </el-select>
            </div>
            <div class="title">
              <el-tag>解析:</el-tag>
              <el-input
                type="textarea"
                rows="2"
                v-model="form.analysis"
                placeholder="请输入答案解析"
                resize="none"
                class="answer" />
            </div>
            <div class="submit">
              <el-button type="primary" @click="submitQuestion">立即添加</el-button>
            </div>
          </div>

          <!-- 填空题部分 -->
          <div class="change fill" v-if="optionValue === '填空题'">
            <div class="title">
              <el-tag>题目:</el-tag>
              <el-input
                type="textarea"
                rows="2"
                v-model="form.content"
                placeholder="请输入题目内容，用____表示填空"
                resize="none"
                class="answer" />
            </div>
            <div class="fillAnswer">
              <span>正确答案:</span>
              <el-input v-model="form.answer" placeholder="请输入正确答案" class="w150" />
            </div>
            <div class="title analysis">
              <el-tag>解析:</el-tag>
              <el-input
                type="textarea"
                rows="2"
                v-model="form.analysis"
                placeholder="请输入答案解析"
                resize="none"
                class="answer" />
            </div>
            <div class="submit">
              <el-button type="primary" @click="submitQuestion">立即添加</el-button>
            </div>
          </div>

          <!-- 判断题部分 -->
          <div class="change judge" v-if="optionValue === '判断题'">
            <div class="title">
              <el-tag>题目:</el-tag>
              <el-input
                type="textarea"
                rows="2"
                v-model="form.content"
                placeholder="请输入题目内容"
                resize="none"
                class="answer" />
            </div>
            <div class="judgeAnswer">
              <span>正确答案:</span>
              <el-radio-group v-model="form.answer">
                <el-radio label="正确">正确</el-radio>
                <el-radio label="错误">错误</el-radio>
              </el-radio-group>
            </div>
            <div class="title">
              <el-tag>解析:</el-tag>
              <el-input
                type="textarea"
                rows="2"
                v-model="form.analysis"
                placeholder="请输入答案解析"
                resize="none"
                class="answer" />
            </div>
            <div class="submit">
              <el-button type="primary" @click="submitQuestion">立即添加</el-button>
            </div>
          </div>
        </section>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const activeName = ref('first')
const options = [
  { value: '选择题', label: '选择题' },
  { value: '填空题', label: '填空题' },
  { value: '判断题', label: '判断题' }
]
const letters = ['A', 'B', 'C', 'D']
const optionValue = ref('选择题')

// 表单数据统一存储
const form = reactive({
  subject: '',
  type: 1,
  content: '',
  score: null,
  answer: '',
  analysis: '',
  remark: ''
})
// 选择题选项
const choiceOptions = reactive(['', '', '', ''])

function submitQuestion() {
  // 设置 type
  form.type = optionValue.value === '选择题' ? 1 : optionValue.value === '填空题' ? 0 : 2

  if(form.content.length <= 0){
    ElMessage.error('请输入题目')
    return
  }
  if(form.answer.length <= 0){
    ElMessage.error('请输入答案')
    return
  }

  // 构建请求体
  const payload = { ...form }
  if (form.type === 1) {
    // 选择题
    payload.option1 = choiceOptions[0]
    payload.option2 = choiceOptions[1]
    payload.option3 = choiceOptions[2]
    payload.option4 = choiceOptions[3]
    if(payload.option1.length <= 0 || payload.option2.length <= 0 || payload.option3.length <= 0 || payload.option4.length <= 0){
      ElMessage.error('请补足选项')
      return
    }
  }

  axios.post('/api/question/add', payload)
    .then(res => {
      if (res.data.code === 200) {
        ElMessage.success('添加成功')
        resetForm()
      } else {
        ElMessage.error(res.data.message || '添加失败')
      }
    })
    .catch(() => ElMessage.error('请求出错'))
}

function resetForm() {
  form.subject = ''
  form.content = ''
  form.score = null
  form.answer = ''
  form.analysis = ''
  form.remark = ''
  choiceOptions.splice(0, 4, '', '', '', '')
}
</script>

<style scoped>
.add { overflow: hidden; }
.append { margin: 20px; }
ul { display: flex; flex-wrap: wrap; margin-bottom: 10px; }
li { margin-right: 20px; display: flex; align-items: center; }
.w150 { width: 150px; margin-left: 6px; }
.change { background: #E7F6F6; padding: 16px; border-radius: 4px; margin-top: 20px; }
.options ul { display: flex; flex-direction: column; }
.options li { margin: 6px 0; display: flex; align-items: center; }
.answer { margin-left: 10px; width: 80%; }
.submit { text-align: center; margin-top: 20px; }
</style>
